package org.jeecg.modules.lab.sample.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.cronJob.MyEnum;
import org.jeecg.common.cronJob.entity.LabCronJob;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.lab.cronJob.service.ILabCronJobService;
import org.jeecg.modules.lab.entrust.entity.LabEntrustSample;
import org.jeecg.modules.lab.entrust.service.ILabEntrustSampleService;
import org.jeecg.modules.lab.entrust.vo.LabSamplePage;
import org.jeecg.modules.lab.sample.entity.LabSampleRecord;
import org.jeecg.modules.lab.sample.record.service.ILabSampleRecordService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 样品查询相关功能
 * @Author: jeecg-boot
 * @Date:   2023-08-08
 * @Version: V1.0
 */
@Api(tags="样品查询相关功能")
@RestController
@RequestMapping("/sample/labSample")
@Slf4j
public class LabSampleController extends JeecgController<LabEntrustSample, ILabEntrustSampleService> {
	@Autowired
	private ILabEntrustSampleService labSampleService;
	@Autowired
	private ILabCronJobService labCronJobService;
	@Autowired
	private ILabSampleRecordService labSampleRecordService;
	
	/**
	 * 自定义sql查询样品相关信息
	 *
	 * @param
	 * @param pageNo
	 * @param pageSize
	 * @param
	 * @return
	 */
	//@AutoLog(value = "样品流转记录表-分页列表查询")
	@ApiOperation(value="样品信息表-分页列表查询", notes="样品信息表-分页列表查询")
	@GetMapping(value = "/sampleList")
	public Result<IPage<LabSamplePage>> queryPageList(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
													  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
													  @RequestParam(name="samName",defaultValue="") String samName,
													  @RequestParam(name="samNum",defaultValue="") String samNum) {
		List<LabSamplePage> samplePageList = labSampleService.querySampleList(samName,samNum);
		List<LabSamplePage> PageList = samplePageList.stream().skip((pageNo - 1) * pageSize)
				.limit(pageSize)
				.collect(Collectors.toList());
		Page<LabSamplePage> page = new Page<LabSamplePage>(pageNo, pageSize);
		page.setTotal(samplePageList.size());
		page.setRecords(PageList);
		return Result.OK(page);
	}


	/**
	 * 样品领用
	 *
	 * @param labSamplePage
	 * @return
	 */
	@ApiOperation(value="样品回收-样品领用", notes="样品回收-样品领用")
	@RequestMapping(value = "/sampleOut", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<LabSampleRecord> sampleOut(@RequestBody LabSamplePage labSamplePage) {
		LabEntrustSample labSample = new LabEntrustSample();
		BeanUtils.copyProperties(labSamplePage, labSample);
		LabEntrustSample labSampleEntity = labSampleService.getById(labSample.getId());
		if(labSampleEntity==null) {
			return Result.error("未找到对应数据");
		}
		labSample.setSamStatus("在检");
		labSample.setSynFlag("1");
		labSampleService.updateById(labSample);

		// todo 样品流转记录
		LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		labSampleRecordService.addOutRecords(labSample,loginUser);

		if (MyEnum.sync) {
			long time = new Date().getTime();
			List<LabCronJob> jobs = new ArrayList<>();
			jobs.add(new LabCronJob("lab_entrust_sample", "1", time, 1));
			jobs.add(new LabCronJob("lab_sample_record", "0", time, 2));
			labCronJobService.saveBatch(jobs);
		}
		return Result.OK("领样成功!");
	}


	/**
	 * 样品回收
	 *
	 * @param labSamplePage
	 * @return
	 */
	@ApiOperation(value="样品回收-根据id更新样品状态", notes="样品回收-根据id更新样品状态")
	@RequestMapping(value = "/sampleBack", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<LabSampleRecord> sampleBack(@RequestBody LabSamplePage labSamplePage) {
		LabEntrustSample labSample = new LabEntrustSample();
		BeanUtils.copyProperties(labSamplePage, labSample);
		LabEntrustSample labSampleEntity = labSampleService.getById(labSample.getId());
		if(labSampleEntity==null) {
			return Result.error("未找到对应数据");
		}
		labSample.setSamStatus("备样");
		labSample.setSynFlag("1");
		labSampleService.updateById(labSample);

		// todo 样品流转记录
		LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		labSampleRecordService.addBackRecords(labSample,loginUser);

		if (MyEnum.sync) {
			long time = new Date().getTime();
			List<LabCronJob> jobs = new ArrayList<>();
			jobs.add(new LabCronJob("lab_entrust_sample", "1", time, 1));
			jobs.add(new LabCronJob("lab_sample_record", "0", time, 2));
			labCronJobService.saveBatch(jobs);
		}
		return Result.OK("回收成功!");
	}


	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "样品流转记录表-通过id查询")
	@ApiOperation(value="样品流转记录表-通过id查询", notes="样品流转记录表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<LabSampleRecord> queryById(@RequestParam(name="id",required=true) String id) {
//		LabSampleRecord labSampleRecord = labSampleService.getById(id);
//		if(labSampleRecord==null) {
//			return Result.error("未找到对应数据");
//		}
		return Result.OK(null);
	}



}
